
var host = 'http://haishi.5imakeup.com:3002/api/'
angular.module('app.controllers', ['ionic'])
  
.controller('page1Ctrl', function($scope,$http,$state) {
    var url = host + 'ShipQuery';
    $scope.DoQuery = function(keyword,registerNO) {
        var param = {
            keyword:keyword,
            registerNO:registerNO
        }
        $http.post(url,param).success(function(data) {
           if(data.IsSuccess) {
               $scope.Ships = data.Data;
           } 
        });
    };
    
    $scope.goDetail = function(ID) {
        $state.go('page9',{ShipID:ID});
    }
    
    $scope.AddNew = function() {
        $state.go('page11');
    }
    
    $scope.modify = function(ID) {
         $state.go('page12',{ShipID:ID});
    }
    
    $scope.Delete = function(obj) {
        var ID = obj.ID;
        var url = host + 'Ships?ShipID=' + ID;;
        $http.delete(url).success(function(data) {
           if(data.IsSuccess) {
               alert('删除成功');
               var newShips = RemoveObjectFromArray($scope.Ships,obj);

               $scope.Ships = newShips;
               $scope.$apply();
           } 
        });
    }
    
    $scope.AddStaff = function(ID) {
        $state.go('page13',{ShipID:ID});
    }
    
    $scope.AddCert = function(ID) {
        $state.go('page14',{ShipID:ID});
    }
    
    $scope.IsFromWeb = window.isFromWeb;
})
   

   
.controller('page2Ctrl', function($scope,$http,$state) {
    var url = host + 'ShipQuery';
    $scope.DoQuery = function(keyword,registerNO) {
        var param = {
            keyword:keyword,
            registerNO:registerNO
        }
        $http.post(url,param).success(function(data) {
           if(data.IsSuccess) {
               $scope.Ships = data.Data;
           } 
        });
    };
    
    $scope.goDetail = function(ID) {
        $state.go('page10',{ShipID:ID});
    }
    
    $scope.modify = function(ID) {
        $state.go('page15',{ShipID:ID});
    }
    
    $scope.IsFromWeb = window.isFromWeb;
})

.controller('page3Ctrl', function($scope,$http,$state) {
    var url = host + 'ShipQuery';
    $scope.DoQuery = function(keyword,registerNO) {
        var param = {
            keyword:keyword,
            registerNO:registerNO
        }
        $http.post(url,param).success(function(data) {
           if(data.IsSuccess) {
               $scope.Ships = data.Data;
           } 
        });
    };
    
    $scope.goDetail = function(ID) {
        $state.go('page7',{ShipID:ID});
    }
    
    $scope.modify = function(ID) {
        $state.go('page16',{ShipID:ID});
    }
    
    $scope.IsFromWeb = window.isFromWeb;
})
      
.controller('page4Ctrl', function($scope,$http,$state,$rootScope) {
    $scope.isLoginFailed = false;
    $scope.username='';
    $scope.password='';
    $scope.login = function(username,password) {
        var userName = username;
        var password = password;
        var url = host + 'Login';
        var param = {
            UserName:userName,
            Password:password
        };
        
        $http.post(url,param).success(function(data){
            if(data.IsSuccess) {
                if(data.Data.IsLogined) {
                    $rootScope.IsAdmin=data.Data.IsAdmin;
                    $state.go('side-menu21.page1');
                } else {
                    $scope.isLoginFailed = true;
                }
            } else {
                $scope.isLoginFailed = true;
            }
            
        }).error(function(err) {
            $scope.isLoginFailed = true;
        });
       
    };
    
})
   
.controller('page7Ctrl', function($scope,$http,$location,$ionicHistory) {
    var queryString = $location.search();
    var shipID = queryString['ShipID'];
    var url = host + 'ShipCert?ShipID=' + shipID;
    $http.get(url).success(function(data) {
        if(data.IsSuccess) {
            for(var i=0;i<data.Data.length;i++) {
                if(data.Data[i]==0) {
                    data.Data[i].IsParmentStr = '否';
                } else {
                    data.Data[i].IsParmentStr = '是';
                }
            }
                
            $scope.Certs = data.Data;
            
        }
    });
    
    $scope.goBack = function() {
        $ionicHistory.goBack();
    }
})

.controller('page9Ctrl',function($scope,$http,$location,$state,$ionicHistory){
    var queryString = $location.search();
    var shipID = queryString['ShipID'];
    var url = host + 'Ships?ShipID=' + shipID;
    console.log(url);
    $http.get(url).success(function(data) {
        if(data.IsSuccess) {
            if(data.Data.length > 0){
                $scope.Data = data.Data[0];
            }
        }
    });
    
    $scope.goBack = function() {
        $ionicHistory.goBack();
    }
    
    $scope.ShowStaff = function(ID) {
         $state.go('page10',{ShipID:ID});
    }
    
    $scope.ShowCert = function(ID) {
         $state.go('page7',{ShipID:ID});
    }
})

.controller('page10Ctrl', function($scope,$http,$location,$ionicHistory) {
    var queryString = $location.search();
    var shipID = queryString['ShipID'];
   
    var url = host + 'ShipStaff?ShipID=' + shipID;
    console.log(url);
     $http.get(url).success(function(data) {
        if(data.IsSuccess) {   
            $scope.Staffs = data.Data;
        }
    });
    
    $scope.goBack = function() {
        $ionicHistory.goBack();
    }
})

.controller('page11Ctrl', function($scope,$http,$location,$ionicHistory) {
    $scope.Data = {};
    $scope.goBack = function() {
        $ionicHistory.goBack();
    }
    
    $scope.Submit = function() {
        var fields = JSON.stringify($scope.Data);
        var params = JSON.parse(fields);
        var url = host + 'Ships';
        $http.post(url,params).success(function(result) {
            if(result.IsSuccess) {
                alert('新增成功');
                $ionicHistory.goBack();
            }
           
        });
        
    }
})

.controller('page12Ctrl', function($scope,$http,$location,$ionicHistory) {
    var queryString = $location.search();
    var shipID = queryString['ShipID'];
    var url = host + 'Ships?ShipID=' + shipID;
    console.log(url);
    $http.get(url).success(function(data) {
        if(data.IsSuccess) {
            if(data.Data.length > 0){
                $scope.Data = data.Data[0];
            }
        }
    });
    
    $scope.goBack = function() {
        $ionicHistory.goBack();
    }
    
    $scope.Submit = function() {
        var fields = JSON.stringify($scope.Data);
        var params = JSON.parse(fields);
        params.ID = shipID;
        var url = host + 'Ships';
        $http.put(url,params).success(function(result) {
            if(result.IsSuccess) {
                alert('修改成功');
                $ionicHistory.goBack();
            }
           
        });
        
    }
})

.controller('page13Ctrl', function($scope,$http,$location,$ionicHistory) {
    var queryString = $location.search();
    var shipID = queryString['ShipID'];
    var url = host + 'Ships?ShipID=' + shipID;
    
    $scope.goBack = function() {
        $ionicHistory.goBack();
    }
    
    $scope.Staff = {};
    $scope.Submit = function() {
        var fields = JSON.stringify($scope.Staff);
        var params = JSON.parse(fields);
        params.ShipID = shipID;
        var url = host + 'ShipStaff';
        $http.post(url,params).success(function(result) {
            if(result.IsSuccess) {
                alert('新增成功');
                $ionicHistory.goBack();
            }
        });
    }
})


.controller('page14Ctrl', function($scope,$http,$location,$ionicHistory) {
    var queryString = $location.search();
    var shipID = queryString['ShipID'];
    var url = host + 'Ships?ShipID=' + shipID;
    
    $scope.goBack = function() {
        $ionicHistory.goBack();
    }
    
    $scope.Cert = {};
    $scope.Submit = function() {
        var fields = JSON.stringify($scope.Cert);
        var params = JSON.parse(fields);
        
        params.ShipID = shipID;
        params.IsParment = params.IsParmentStr;
        var url = host + 'ShipCert';
        $http.post(url,params).success(function(result) {
            if(result.IsSuccess) {
                alert('新增成功');
                $ionicHistory.goBack();
            }
        });
    }
})

.controller('page15Ctrl', function($scope,$http,$location,$ionicHistory,$state) {
    var queryString = $location.search();
    var shipID = queryString['ShipID'];
   
    var url = host + 'ShipStaff?ShipID=' + shipID;
    console.log(url);
     $http.get(url).success(function(data) {
        if(data.IsSuccess) {   
            $scope.Staffs = data.Data;
        }
    });
    
    $scope.goBack = function() {
        $ionicHistory.goBack();
    }
    
    $scope.SureModeify = function(obj) {
        var url = host + 'ShipStaff';
        $http.put(url,obj).success(function(result) {
            if(result.IsSuccess) {
                alert('修改成功');
                $ionicHistory.goBack();
            }
        });
    }
    
    $scope.Delete = function(obj) {
        var url = host + 'ShipStaff?StaffID='+obj.ID;
         $http.delete(url).success(function(result) {
            if(result.IsSuccess) {
                alert('删除成功');
                $scope.Staffs =RemoveObjectFromArray($scope.Staffs,obj);
            }
        });
    }
    
    $scope.Add = function() {
        $state.go('page13',{ShipID:shipID});
    }
})

.controller('page16Ctrl', function($scope,$http,$location,$ionicHistory,$state) {
    var queryString = $location.search();
    var shipID = queryString['ShipID'];
   
    var url = host + 'ShipCert?ShipID=' + shipID;
    console.log(url);
     $http.get(url).success(function(data) {
        if(data.IsSuccess) {   
            $scope.Certs = data.Data;
        }
    });
    
    $scope.goBack = function() {
        $ionicHistory.goBack();
    }
    
    $scope.SureModeify = function(obj) {
        var url = host + 'ShipCert';
        $http.put(url,obj).success(function(result) {
            if(result.IsSuccess) {
                alert('修改成功');
                $ionicHistory.goBack();
            }
        });
    }
    
    $scope.Delete = function(obj) {
        var url = host + 'ShipCert?ShipCertID='+obj.ID;
         $http.delete(url).success(function(result) {
            if(result.IsSuccess) {
                alert('删除成功');
                $scope.Certs =RemoveObjectFromArray($scope.Certs,obj);
            }
        });
    }
    
    $scope.Add = function() {
        $state.go('page13',{ShipID:shipID});
    }
})

.controller('page17Ctrl', function($scope,$http,$location,$ionicHistory,$state) {
    $scope.NewUser = {};
    
    $scope.Users = [];
    var url = host + 'Users';
    $http.get(url).success(function(data){
        if(data.IsSuccess) {
            $scope.Users = data.Data;
        }
    });
    
    $scope.Delete = function(obj) {
        url = url + '?UID=' + obj.UID; 
        $http.delete(url).success(function(data){
            if(data.IsSuccess) {
                alert('删除成功');
                $scope.Users = RemoveObjectFromArray($scope.Users,obj);
            }
       })
    }
    
    $scope.ModifyPassword = function(obj) {
        var uid = obj.UID;
        var newPassword = obj.NewPassword;
        var params = {
            UID:uid,
            Password:newPassword
        };
        $http.put(url,params).success(function(data){
             if(data.IsSuccess) {
                alert('密码修改成功');
                var idx = $scope.Users.indexOf(obj);
                $scope.Users[idx].NewPassword = '';
             }
        });
    }
    
    $scope.AddNew = function() {
        var fields = JSON.stringify($scope.NewUser);
        var params = JSON.parse(fields);
        var url = host + 'Users'
        $http.post(url,params).success(function(result) {
            if(result.IsSuccess) {
                alert('新增成功');
                $scope.NewUser = {};
            }
        });
    };
    
    $scope.goBack = function() {
        $ionicHistory.goBack();
    }
})

.controller('meauCtrl', function($scope,$http,$location,$ionicHistory,$state,$rootScope) {
    var isAdmin = $rootScope.IsAdmin;
    if(isAdmin ==1 && window.isFromWeb) {
        $scope.ShowAdmin = true;
    } else {
        $scope.ShowAdmin = false;
    }
})


function RemoveObjectFromArray(arr,obj) {
    var idx = arr.indexOf(obj);
    var newArray = [];
    if(idx > -1) {
        for(var i=0;i<arr.length;i++) {
            if(i!=idx) {
                newArray.push(arr[i]);
            }
        }
    }
    
    return newArray;
}




 